package com.shop.service;

import javax.servlet.http.HttpServletRequest;

import com.shop.dao.IAdmin;
import com.shop.dao.IRegistration;
import com.shop.dao.impl.AdminDao;
import com.shop.dao.impl.RegistrationDao;
import com.shop.model.Admin;
import com.shop.model.Registration;

public class AccountService {

	/**
	 * @param request
	 * @return
	 */
	public boolean checkAccount(HttpServletRequest request) {
		
		String username = request.getParameter("username");		
		String password = request.getParameter("password");
		String email = request.getParameter("email");
		String name = request.getParameter("name");
		String sex = request.getParameter("sex");
		String birthday = request.getParameter("birthday");
		System.out.println("checkAccount"+username+ password+ email+ name+sex+birthday);
		// 检查账户是否已经存在
		// 初始化dao层
		IAdmin ad = new AdminDao();
		IRegistration rst = new RegistrationDao();
		Integer i = ad.checkAcc(username);
		if (i > 0) {
			System.out.println("账户已经存在");
			return false;
		}else {
			// 账户不存在，进行开户,保存至数据库表中
			Admin a = new Admin(username, password);
			Registration r = new Registration(username, password, email, name, sex, birthday);
			//调用dao层方法
			Integer i1 = ad.insert(a);
			Integer i2 = rst.insert(r);
			System.out.println(i1);
			System.out.println(i2);
			if (i1 != null && i2 != null) {
				System.out.println("插入数据成功");
			}else {
				System.out.println("插入数据失败");
			}
			return true;	
		}
		
	}
	
}
